import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    component: () => import("../views/Home.vue"),
    children: [
      {
        path: "/home",
        redirect: "/home/index",
      },
      {
        path: "/home/index",
        component: () => import("../views/home/Index.vue"),
        children: [
          {
            path: "/home/index",
            redirect: "/home/index/recommend",
          },
          {
            path: "/home/index/recommend",
            component: () => import("../views/home/index/Recommend.vue"),
          },
          {
            path: "/home/index/soon",
            component: () => import("../views/home/index/Soon.vue"),
          },
          {
            path: "/home/index/ranking",
            component: () => import("../views/home/index/Ranking.vue"),
          },
          {
            path: "/home/index/video",
            component: () => import("../views/home/index/Video.vue"),
          },
        ],
      },
      {
        path: "/home/find",
        component: () => import("../views/home/Find.vue"),
      },
      {
        path: "/home/dynamic",
        component: () => import("../views/home/Dynamic.vue"),
        children: [
          {
            path: "/home/dynamic",
            redirect: "/home/dynamic/attention",
          },
          {
            path: "/home/dynamic/attention",
            component: () => import("../views/home/dynami/Attention.vue"),
          },
          {
            path: "/home/dynamic/recommend",
            component: () => import("../views/home/dynami/Recommend.vue"),
          },
          {
            path: "/home/dynamic/forum",
            component: () => import("../views/home/dynami/Forum.vue"),
          },
        ],
      },
      {
        path: "/home/message",
        component: () => import("../views/home/Message.vue"),
        meta: {
          requireLogin: true,
        },
      },
      {
        path: "/home/mygame",
        component: () => import("../views/home/MyGame.vue"),
        children: [
          {
            path: "/home/mygame",
            redirect: "/home/mygame/games",
          },
          {
            path: "/home/mygame/games",
            component: () => import("../views/home/mygame/Games.vue"),
            children: [
              {
                path: "/home/mygame/games",
                redirect: "/home/mygame/games/local",
              },
              {
                path: "/home/mygame/games/local",
                component: () => import("@/views/home/mygame/games/Local.vue"),
              },
              {
                path: "/home/mygame/games/update",
                component: () =>
                  import("../views/home/mygame/games/Update.vue"),
              },
              {
                path: "/home/mygame/games/played",
                component: () =>
                  import("../views/home/mygame/games/Played.vue"),
              },
            ],
          },
          {
            path: "/home/mygame/cloudplay",
            component: () => import("../views/home/mygame/Cloudplay.vue"),
          },
          {
            path: "/home/mygame/appointment",
            component: () => import("../views/home/mygame/Appointment.vue"),
          },
        ],
      },
    ],
  },
  {
    path: "/detail/:id",
    component: () => import("../views/Detail.vue"),
    props: true,
  },
  {
    path: "/login",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/search",
    component: () => import("../views/Search.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("../views/NotFound.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.meta.requireLogin) {
    if (localStorage.getItem("tel")) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

export default router;
